<template>
    <div id="app">
        <el-alert
                closable=false
                @close="alertNotShow"
                v-show="alertShow"
                :title="alertTitle"
                type="error"
                show-icon
        >
        </el-alert>
        <div id="head"></div>
        <div id="title">wanmarecruit管理员登录</div>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="name">
                <el-input type="text" v-model="ruleForm.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="验证码" prop="code">
                <el-input type="text" v-model="ruleForm.code" style="width: 200px;"></el-input>
                <img :src="codeImg" @click="getNewCode" style="position: relative;top:10px;width: 100px;"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>


<script>
    export default {
        name: "login",
        data() {
            var validateName = (rule, value, callback) => {
                if (value==undefined ||value === '') {
                    callback(new Error('请输入用户名'));
                } else {
                    callback();
                }
            };
            var validatePass = (rule, value, callback) => {
                if (value==undefined ||value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    callback();
                }
            };
            var validateCode = (rule, value, callback) => {
                if (value==undefined ||value === '') {
                    callback(new Error('请输入验证码'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    name:'',
                    pass: '',
                    code:''
                },

                codeImg:'',
                codeKey:'',
                alertTitle:'',
                rules: {
                    name: [
                        { validator: validateName, trigger: 'blur' }
                    ],
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    code: [
                        { validator: validateCode, trigger: 'blur' }
                    ]
                },
                alertShow:false
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios.get("manage/checkCode",{params:{code:this.ruleForm.code,codeKey:this.codeKey}}).then(response=>{
                            if (response.data.code==10002)
                            {
                                this.alertTitle='您输入的验证码有误';
                                this.alertShow=true;
                                return;
                            }
                            if (response.data.code==10003)
                            {
                                this.alertTitle='当前验证码已经失效，请点击图片刷新验证码';
                                this.alertShow=true;
                                return;
                            }
                            this.$axios.post("manage/tryLogin", this.ruleForm)
                                .then(response => {
                                    console.log(response);
                                    if (response.data.code == 10000) {
                                        this.alertTitle = '您输入的用户名或密码有误';
                                        this.alertShow = true;
                                        return;
                                    }
                                    window.localStorage.setItem("token", response.data.token);
                                    this.$store.commit("setAdmin",response.data.loginManager);
                                    this.$router.push("/manage/index");
                                });
                        });
                        }
                     else {
                        console.log('提交失败');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            alertNotShow(){
                this.alertShow=false;
            },
            getNewCode(){
                this.$axios.get("manage/code").then(response=>{
                    this.codeImg=response.data.codeImg;
                    this.codeKey = response.data.codeKey;
                })
            }


        },
        created() {
            this.getNewCode();
        }

    }
</script>

<style scoped>
    .el-alert{
        width: 300px;
        height: 50px;
        top: 50px;
        margin: auto;
    }
    html{
        height: 100%;
    }
    body{
        height: 100%;
    }
    #app{
        height: 100%;
        background-color: #e6f0f9;
    }
    #head{
        height: 100px;
    }
    .el-form{
        border: 1px black solid;
        border-radius: 15%;
        width: 500px;
        height: 300px;
        margin: auto;
    }
    #title{
        text-align: center;
        /*margin-top: 100px;*/
        font-size: 30px;
        font-family: '宋体';
        font-weight: bold;
        /*height: 50px;*/
    }
    .el-input{
        width: 300px;
    }
    .el-form-item{
        margin-top:50px;
    }
</style>